package com.tang.plugin;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.tang.plugin.ui.RealConfigurePanel;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class TemplateConfigurable implements SearchableConfigurable {

    private OnApplyListener applyListener;
    private RealConfigurePanel panelConfig;

    public TemplateConfigurable() {
    }

    public TemplateConfigurable(OnApplyListener applyListener) {
        this.applyListener = applyListener;
    }

    @Override
    public @Nullable
    JComponent createComponent() {
        panelConfig = new RealConfigurePanel();
        return panelConfig;
    }

    @Override
    public void apply() throws ConfigurationException {
        panelConfig.cacheConfig();
        panelConfig.saveConfig();
        if (applyListener != null) {
            applyListener.onApply();
        }
    }

    @Override
    public boolean isModified() {
        return panelConfig.isModified();
    }

    @Override
    public @NotNull
    @NonNls
    String getId() {
        return "preferences.ModuleTemplateConfig";
    }

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "Module Template Settings";
    }

    public interface OnApplyListener {
        void onApply();
    }
}
